package com.wman.sheep.common.utils;

/**
 * Created by chenwei on 14-9-16.
 */
public class TextUtil {

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0 || str.equals("null");
    }

    public static boolean isZero(String str) {
        return "0".equals(str) || "00".equals(str) || "0.0".equals(str) || "免费".equals(str);
    }

    public static String getSub(String s, int len) {
        if (s != null && s.length() >= len)
            return s.substring(0, len);
        return s;
    }

    public static String ignoreNull(String str) {
        if (str == null || "null".equals(str))
            return "";
        return str;
    }

    public static String ignoreLastFH(String str) {
        if (isEmpty(str))
            return "";
        char c = str.charAt(str.length() - 1);
        if (c == ',' || c == '。' || c == '.' || c == '，')
            str = str.substring(0, str.length() - 1);
        return str;
    }

    /**
     * 组合多个字符段，用div分隔，忽略空字符串
     *
     * @param div
     * @param strings
     * @return
     */
    public static String combine(String div, String... strings) {
        if (strings == null || strings.length == 0) return "";
        StringBuilder builder = new StringBuilder();
        int i = 0;
        while (i < strings.length && TextUtil.isEmpty(strings[i])) {
            i++;
        }
        if (i < strings.length)
            builder.append(strings[i]);
        while (++i < strings.length) {
            if (!TextUtil.isEmpty(strings[i]))
                builder.append(div).append(strings[i]);
        }
        return builder.toString();
    }

    public static boolean isBeginWithUpperCase(String str) {
        return !TextUtil.isEmpty(str) && str.charAt(0) <= 'Z' && str.charAt(0) >= 'A';
    }

    public static String ignoreLastShi(String city_name) {
        if (city_name != null && city_name.endsWith("市")) {
            int len = city_name.length() - 1;
            city_name = city_name.substring(0, len);
        }
        return city_name;
    }

    public static String subZero(String s) {
        if (TextUtil.isEmpty(s))
            return "";
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");
            s = s.replaceAll("[.]$", "");
        }
        return s;
    }

    public static boolean isDecimal(String str) {
        if (TextUtil.isEmpty(str)) return false;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != '.' && (str.charAt(i) > '9' || str.charAt(i) < '0')) {
                return false;
            }
        }
        return true;
    }
}
